<?php 
/**
* 品牌管理控制器
*/
class BrandsController extends AuthController{
	private $model;
	public function __init(){
		parent::__init();
		$this->model = K('Brands');
	}

	public function index(){
		// $data = $this->model->all();
		$data = M()->join('__brands__ b JOIN __brands_cate__ bc ON b.brands_cate_bcid=bc.bcid')->all();
		// p($data);
		$this->assign('data',$data);
		$this->display();
	}

	// 添加品牌
	public function add(){
		// 获得品牌分类数据
		$brandsCate = K('BrandsCate')->all();
		// p($brandsCate);
		$this->assign('brandsCate',$brandsCate);

		if(IS_POST){
			if(!$this->model->addData()){
				$this->error($this->model->error);
			}
			$this->success('添加成功',U('index'));
		}
		$this->display();
	}

	// 编辑品牌
	public function edit(){
		$bid = Q('get.bid',0,'intval');
		// 获得旧数据
		$oldData = $this->model->where("bid={$bid}")->find();
		// p($oldData);
		$this->assign('oldData',$oldData);

		// 获得品牌分类数据
		$brandsCate = K('BrandsCate')->all();
		// p($brandsCate);
		$this->assign('brandsCate',$brandsCate);

		if(IS_POST){
			if(!$this->model->upData()){
				$this->error($this->model->error);
			}
			$this->success('修改成功',U('index')); 
		}
		$this->display();
	}

	// 删除品牌
	public function del(){
		$bid = Q('get.bid',0,'intval');
		$this->model->where("bid={$bid}")->delete();
		$this->success('删除成功',U('index'));
	}

}
 ?>